package org.example.camundaapp.global;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.ProcessEngineException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 * @className: GlobalExceptionHandler
 * @author: ZHRJ
 * @date: 2025-07-07 10:15
 * @Version: 1.0
 * @description:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ProcessEngineException.class)
    public ResponseEntity<String> handleEngineException(ProcessEngineException e) {
        log.error("Camunda流程异常: ", e);
        // 处理乐观锁异常（并发场景）
        if (e.getMessage().contains("optimistic locking")) {
            return ResponseEntity.status(409).body("操作冲突，请重试");
        }
        return ResponseEntity.badRequest().body(e.getMessage());
    }

    // 添加自定义业务异常处理
    /*@ExceptionHandler(BusinessException.class)
    public ResponseEntity<String> handleBusinessException(BusinessException e) {
        return ResponseEntity.badRequest().body(e.getMessage());
    }*/

    // 其他Camunda异常处理

}
